﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Collections.Generic;
using STHPMS1.ICore.I_Serv;
using STHPMS1.Common.C_Enum;

namespace STHPMS1.Web.W_Controller.Decision.Fondos
{
    [RoutePrefix("api/dfmaincost")]
    public class DF_MaincostController : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetdfmaincostByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string anni_name = null, string analy_year = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<Df_Main_Cost, bool>> exp = null;
            if (anni_name != null && anni_name.ToString().Length > 0 )
                exp = pre => pre.Anni_Name == anni_name.ToString();

            if (anni_name != null && anni_name.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anni_name.ToString() && pre.Analy_Year==analy_year.ToString();

            var data = this.Scene.Bll.Df_Main_Cost.Get<Df_Main_Cost>(exp, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        /// <summary>
        /// 同步数据 资金不受限养护维修费用
        /// </summary>
        /// <param name="anniname"></param>
        [Route("SetInsert")]
        [HttpGet]
        public void SetInsert(string anniname,string analy_year)
        {
            this.Scene.Bll.Df_Main_Cost.Insert(anniname, analy_year);

            #region 转移到BLL层
            //IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();

            //ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            //dao.Delete<Df_Main_Cost>(p => p.Anni_Name == anniname && p.Analy_Year==analy_year);  //资金模型名称


            //Expression<Func<Df_Costmod_Para, bool>> exp = null;
            //if (anniname != null && anniname.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
            //    exp = pre => pre.Anni_Name == anniname.ToString() && pre.Analy_Year==analy_year;

            //List<Df_Costmod_Para> data = this.Scene.Bll.Df_Costmod_Para.Get<Df_Costmod_Para>(exp, null).ToList(); //资金模型参数  理论上只有一条数据
            

            //int i = 0;

            //foreach(Df_Costmod_Para info in data)
            //{
            //    List<Dn_Divi_Planning> list = dao.Df_Main_Cost.GetDivi<Dn_Divi_Planning>(info.Roadnet_Name,info.Analy_Year);

            //    foreach (Dn_Divi_Planning diviinfo in list)
            //    {
            //        Df_Main_Cost maininfo = new Df_Main_Cost();
            //        maininfo.Id = serv.NewGuid();
            //        maininfo.Anni_Name = anniname;
            //        maininfo.Roadnet_Name = info.Roadnet_Name;
            //        maininfo.Rdwayid = diviinfo.Rdwayid;
            //        maininfo.Mainsegid = diviinfo.Mainsegid;
            //        maininfo.Beginmp = diviinfo.Beginmp;
            //        maininfo.Endmp = diviinfo.Endmp;
            //        //该路段为大中修养护，当年养护后，以后每年则进行日常性养护
            //        if ((i >= 1 && diviinfo.Segid_Main == 12002) || (i >= 1 && diviinfo.Segid_Main == 12003))
            //        {
            //            maininfo.Segid_Main = 12000;
            //        }
            //        maininfo.Main_Meas = "";
            //        maininfo.Carry_Year = info.Analy_Year;
            //        maininfo.Analy_Year = info.Analy_Year;

            //        if (diviinfo.Segid_Main == 12000) //该路段为日常养护
            //        {
            //            maininfo.Segid_Main = 12000;
            //        }
            //        if (diviinfo.Segid_Main == 12001)   //该路段为预防性养护，判断是否单年养护，如果不是则进行日常性养护
            //        {
            //            string mainyear = dao.Dp_Preven_Detail.GetMainYear(diviinfo.Mainsegid);
            //            if (info.Analy_Year == mainyear)
            //            {

            //                maininfo.Segid_Main = 12001;
            //            }
            //            else
            //            {
            //                maininfo.Segid_Main = 12000;
            //            }
            //        }
            //        if ((i == 0 && diviinfo.Segid_Main == 12002) || (i == 0 && diviinfo.Segid_Main == 12003))  //该路段为大中修
            //        {
            //            maininfo.Segid_Main = diviinfo.Segid_Main;
            //        }


            //        if (maininfo.Segid_Main == 12000)  //计算日常养护费用
            //        {
            //            double lenght = Convert.ToDouble(diviinfo.Endmp - diviinfo.Beginmp);
            //            //通过决策路段获取基础路段信息
            //            IB_Invntory b_invntoryInfo = this.Scene.Bll.Dp_Dailymain_Cost.GetB_Invntory(diviinfo.Mainsegid);
            //            int Year = (DateTime.Now - Convert.ToDateTime(b_invntoryInfo.Begindate)).Days / 365;

            //            double cost = dao.Df_Dailymian_Cost.GetDaily_Unitcost(Convert.ToInt32(b_invntoryInfo.Pvmttype_Dictid), b_invntoryInfo.Rdclass_Dictid.ToString(), Year);  //通车年限？？？？
            //            maininfo.Segid_Maincost = lenght / 1000 * cost;
            //        }
            //        if (maininfo.Segid_Main == 12001) //计算预防性养护费用
            //        {
            //            maininfo.Segid_Maincost = dao.Dp_Preven_Detail.GetCost(diviinfo.Mainsegid);
            //        }
            //        if (maininfo.Segid_Main == 12002) //计算中修费用
            //        {
            //            maininfo.Segid_Maincost = 10;   //??
            //        }
            //        if (maininfo.Segid_Main == 12003) //计算大修费用
            //        {
            //            maininfo.Segid_Maincost = 100;  //??
            //        }
            //        dao.Save<Df_Main_Cost>(maininfo);
            //    }

            //    i++;
            //}
            #endregion
        }
    }
}